package com.zzx.project.controller;

import com.alibaba.fastjson.JSON;
import com.zzx.project.entity.Article;
import com.zzx.project.entity.Maxim;
import com.zzx.project.entity.Websitelm;
import com.zzx.project.service.ArticleService;
import com.zzx.project.service.MaximService;
import com.zzx.project.service.WlmService;
import com.zzx.project.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;

//首页加载Controller
@Slf4j
@Controller
public class InitController {

    private ArticleService articleService;
    private MaximService maximService;
    private WlmService wlmService;

    @Autowired
    public void setArticleService(ArticleService articleService) {
        this.articleService = articleService;
    }

    @Autowired
    public void setMaximService(MaximService maximService) {
        this.maximService = maximService;
    }

    @Autowired
    public void setWlmService(WlmService wlmService) {
        this.wlmService = wlmService;
    }

    /**
     * <p>首页初始化</p>
     *
     * @param request
     * @param session
     * @param model
     * @return java.lang.String
     * @author zhouzhixin
     * @date 2021/5/8 9:35
     */
    @RequestMapping("/")
    public String mainInit(HttpServletRequest request, HttpSession session, Model model) {
        //初始化session属性对象
        Maxim maxim = null;
        Article firstArticleOfLevelThree = null;
        List<Article> notFirstArticleOfLevelThree = null;
        List<Article> articleLOne = null;
        List<Article> hotArticles = null;
        List<Article> randomArticle1 = null;
        List<Websitelm> fiveData = null;
        Integer levelThreeSize = null;

        //判断每日格言是否为空
        if (null == session.getAttribute("maxim")) {
            maxim = maximService.getNewMaxim();
            log.info("获取到的每日格言:{}", JSON.toJSONString(maxim));
        }

        //判断轮播图Level3文章第一条、Level为3的文章数、Level3的后两篇文章是否为空
        if (null == session.getAttribute("firstArticleOfLevelThree") || null == session.getAttribute("articleLThree2Count") || null == session.getAttribute("articleLThree2")) {
            notFirstArticleOfLevelThree = articleService.getLevelThree();
            if (notFirstArticleOfLevelThree.size() > 0) {
                firstArticleOfLevelThree = notFirstArticleOfLevelThree.get(0);
                log.info("获取到的轮播图第一条文章id:{}", JSON.toJSONString(firstArticleOfLevelThree.getArticleid()));
                //由于bootstrap轮播图原因，首条文章需要与后面的文章相分离才能进行展示
                levelThreeSize = notFirstArticleOfLevelThree.size();
                if (notFirstArticleOfLevelThree.size() > 1) {
                    notFirstArticleOfLevelThree.remove(0);
                }
            }
            notFirstArticleOfLevelThree.forEach(article -> log.info("获取到的轮播图其他两条文章id:{}", article.getArticleid()));
        }

        List<Article> levelOne = articleService.getLevelOne();
        articleLOne = new ArrayList<>();
        for (Article article : levelOne) {
            int articleAudiences = article.getArticleaudience().split(",").length - 1;
            article.setArticleaudiences(articleAudiences);
            String[] tags = article.getArticletags().split(",");
            List<String> tagList = new ArrayList<>();
            Collections.addAll(tagList, tags);
            article.setArticletaglist(tagList);
            articleLOne.add(article);
            log.info("获取到的一级文章id:{}", article.getArticleid());
        }

        //判断热门文章是否为空
        hotArticles = articleService.getHotArticle();
        if ((null == session.getAttribute("hotArticle1") || null == session.getAttribute("hotArticle")) && hotArticles.size() > 0) {
            hotArticles.forEach(article -> log.info("获取到的热门文章id:{}", article.getArticleid()));
            session.setAttribute("hotArticle1", hotArticles.get(0));
            hotArticles.remove(0);
        }

        List<Article> randomArticle = (List<Article>) request.getSession().getAttribute("randomArticle");
        //判断每日文章是否为空
        if (null == randomArticle) {
            randomArticle = articleService.getRandomArticle();
            randomArticle1 = new ArrayList<>();
            for (Article article : randomArticle) {
                log.info("获取到的每日文章id:{}", article.getArticleid());
                String[] audiences = article.getArticleaudience().split(",");
                article.setArticleaudiences(audiences.length - 1);
                randomArticle1.add(article);
            }
        }

        int count = 0;
        //获取一级文章数量
        int sum = articleService.getLevelOneCount();
        if (0 != articleService.getLevelOneCount() % 5) {
            count = (sum / 5) + 1;
        }

        //判断五条网站留言是否为空
        if (null == session.getAttribute("fiveData")) {
            fiveData = wlmService.getFiveData();
        }

        String ipAddr = CommonUtil.getIpAddr(request);
        log.info("IPAddress -> 获取来自ip地址:{}主机1的访问", ipAddr);

        //数据处理后统一存放到对应作用域中
        session.setAttribute("maxim", maxim);
        session.setAttribute("firstArticleOfLevelThree", firstArticleOfLevelThree);
        session.setAttribute("levelThreeSize", levelThreeSize);
        session.setAttribute("notFirstArticleOfLevelThree", notFirstArticleOfLevelThree);
        session.setAttribute("hotArticles", hotArticles);
        session.setAttribute("randomArticle", randomArticle1);
        session.setAttribute("fiveData", fiveData);
        model.addAttribute("articleLOne", articleLOne);
        model.addAttribute("count", count);
        model.addAttribute("currPage", 1);
        return "main/index";
    }

}
